/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package book;

import com.toedter.calendar.JDateChooser;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.sql.CallableStatement;
import java.util.Date;
import java.sql.ResultSet;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

public class CheckInOut extends javax.swing.JFrame {

    CallableStatement st;
    ResultSet rs;
    String para;
    private long sum = 0;
    private long t = 0;
    private DefaultTableModel model;
    public String chk_all1 = "^(\\d{0,10}([.]*)\\d+)*$";
    public String chk_all = "^(\\d{0,10})*$";
    public String chk_find = "^(\\d{4}\\-\\d{1,2}\\-\\d{1,2})$";
    public String chk_blank = "^[0-9]+$";
    public String chk_blank1 = "^\\w+$";

    public boolean check(String a, String b, String c) {
        Pattern pat = Pattern.compile(a);
        Matcher mat = pat.matcher(b);
        if (mat.find()) {
            return true;
        } else {
            JOptionPane.showMessageDialog(this, c);
        }
        return false;
    }

    public boolean checkfind() {
        if (check(chk_find, aaa.getText(), "?????????????????") == false) {
            aaa.grabFocus();
            return false;
        }
        return true;
    }

    public boolean input() {
        if (check(chk_blank, txtbook.getText(), "Book ID is number and not blank") == false) {
            txtbook.grabFocus();
            return false;
        }
        if (check(chk_blank1, txtuser.getText(), "User ID is not blank") == false) {
            txtuser.grabFocus();
            return false;
        }
        if (dateborrow.getDate() == null) {
            JOptionPane.showMessageDialog(rootPane, "Date Borrow accordance with the format and not blank");
            dateborrow.grabFocus();
            return false;
        }
        if (datedue.getDate() == null) {
            JOptionPane.showMessageDialog(this, "Date Due accordance with the format and not blank");
            datedue.grabFocus();
            return false;
        }
        if (check(chk_all, txtoverdue.getText(), "Overdue is number and less than 10 characters") == false) {
            txtoverdue.grabFocus();
            return false;
        }
        if (check(chk_all1, txtfine.getText(), "Fine is number and less than 10 characters") == false) {
            txtfine.grabFocus();
            return false;
        }

        return true;
    }

    public boolean update() {
        if (check(chk_blank, txtbook.getText(), "Book ID is number and not blank") == false) {
            txtbook.grabFocus();
            return false;
        }
        if (check(chk_blank1, txtuser.getText(), "User ID is not blank") == false) {
            txtuser.grabFocus();
            return false;
        }
        if (dateborrow.getDate() == null) {
            JOptionPane.showMessageDialog(rootPane, "Date Borrow accordance with the format and not blank");
            dateborrow.grabFocus();
            return false;
        }
        if (datedue.getDate() == null) {
            JOptionPane.showMessageDialog(this, "Date Due accordance with the format and not blank");
            datedue.grabFocus();
            return false;
        }
        if (check(chk_all, txtoverdue.getText(), "Overdue is number and less than 10 characters") == false) {
            txtoverdue.grabFocus();
            return false;
        }
        if (check(chk_all1, txtfine.getText(), "Fine is number and less than 10 characters") == false) {
            txtfine.grabFocus();
            return false;
        }
        if (datereturn.getDate() == null) {
            JOptionPane.showMessageDialog(rootPane, "Date Return accordance with the format and not blank");
            datereturn.grabFocus();
            return false;
        }

        return true;
    }

    public CheckInOut() {
        initComponents();
        CallableStatement st;
        ResultSet rs;
        txtid.setEnabled(false);
        try {
            model = (DefaultTableModel) table.getModel();
            String sql = "{call loaddata}";
            st = ConnectTest.myConnect.cn.prepareCall(sql, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            rs = st.executeQuery();
            if (rs.next()) {
                loadDataToTextFiled(rs);
            }
            loaddata();


        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void loaddata() {
        CallableStatement st;
        ResultSet rs;
        try {

            model.setRowCount(0);
            String sql = "{call loaddata}";
            st = ConnectTest.myConnect.cn.prepareCall(sql, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            rs = st.executeQuery();
            while (rs.next()) {
                model.addRow(new String[]{rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5), rs.getString(6), rs.getString(7), rs.getString(8), rs.getString(9)});
            }
            table.setModel(model);
            st.close();
            rs.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            st = null;
            rs = null;
        }
    }

    public void loadDataToTextFiled(ResultSet rs) {
        try {
            txtid.setText(rs.getString(1));
            txtbook.setText(rs.getString(2));
            txtuser.setText(rs.getString(3));
            dateborrow.setDate(rs.getDate(4));
            datedue.setDate(rs.getDate(5));
            datereturn.setDate(rs.getDate(6));
            txtoverdue.setText(rs.getString(7));
            txtfine.setText(rs.getString(8));
            txtnote.setText(rs.getString(9));


        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void Click() {
        CallableStatement st;
        ResultSet rs;
        try {
            String sql = "{call Click()}";
            st = ConnectTest.myConnect.cn.prepareCall(sql, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            rs = st.executeQuery();
            int k = table.getSelectedRow() + 1;
            rs.absolute(k);
            loadDataToTextFiled(rs);
            rs.close();
            st.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            st = null;
            rs = null;
        }
    }

    public void ClearFiled() {
        txtid.setText("");
        txtbook.setText("");
        txtuser.setText("");
        dateborrow.setDate(null);
        datedue.setDate(null);
        datereturn.setDate(null);
        txtoverdue.setText("");
        txtfine.setText("");
        txtnote.setText("");
    }

    public void update(CallableStatement a) {
        CallableStatement st;

        try {
            String sql = "{call update_check(?,?,?,?,?,?,?,?,?)}";
            st = ConnectTest.myConnect.cn.prepareCall(sql, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            st.setInt(1, Integer.parseInt(txtid.getText()));
            st.setInt(2, Integer.parseInt(txtbook.getText()));
            st.setString(3, txtuser.getText());
            st.setString(4, DateToString(dateborrow.getDate()));
            st.setString(5, DateToString(datedue.getDate()));
            st.setString(6, DateToString(datereturn.getDate()));
            st.setString(7, txtoverdue.getText());
            st.setString(8, txtfine.getText());
            st.setString(9, txtnote.getText());
            st.execute();
            loaddata();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void Delete() {
        CallableStatement st;
        ResultSet rs;
        try {
            String sql = "{call delete_check(?)}";
            st = ConnectTest.myConnect.cn.prepareCall(sql);
            st.setInt(1, Integer.parseInt(txtid.getText()));
            st.execute();
            loaddata();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void Insert(CallableStatement a) {
        CallableStatement st;

        try {
            String sql = "{call Insert_Check(?,?,?,?,?,?,?,?)}";
            st = ConnectTest.myConnect.cn.prepareCall(sql, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            st.setInt(1, Integer.parseInt(txtbook.getText()));
            st.setString(2, txtuser.getText());
            st.setString(3, DateToString(dateborrow.getDate()));
            st.setString(4, DateToString(datedue.getDate()));
            st.setString(5, null);
            st.setString(6, txtoverdue.getText());
            st.setString(7, txtfine.getText());
            st.setString(8, txtnote.getText());
            st.execute();

            loaddata();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static String DateToString(Date a) {
        SimpleDateFormat cover = new SimpleDateFormat("yyyy/MM/dd");
        return cover.format(a);
    }

    public static Long StringToDate(String a) {
        SimpleDateFormat cover = new SimpleDateFormat("dd/MM/yyyy");
        cover.setLenient(true);
        try {
            Date date = cover.parse(a);
            return date.getTime();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public int getOverdue() {
        Calendar calendar = GregorianCalendar.getInstance();
        calendar.setTime(new Date());
        int datereturn = calendar.get(Calendar.YEAR);

        calendar.setTime(datedue.getDate());
        int datedue = calendar.get(Calendar.YEAR);

        return datereturn - datedue;
    }

    public void search() {
        CallableStatement st;
        ResultSet rs;
        try {
            model.setRowCount(0);
            String sql = "{call SearchID(?)}";
            st = ConnectDB.cn.prepareCall(sql);
            st.setString(1, aaa.getText());
            rs = st.executeQuery();
            while (rs.next()) {
                model.addRow(new String[]{rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5), rs.getString(6), rs.getString(7), rs.getString(8), rs.getString(9)});
            }
            st.close();
            rs.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            st = null;
            rs = null;
        }
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtid = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txtuser = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        txtfine = new javax.swing.JTextField();
        butInsert = new javax.swing.JButton();
        butupdate = new javax.swing.JButton();
        butdetele = new javax.swing.JButton();
        jLabel8 = new javax.swing.JLabel();
        aaa = new javax.swing.JTextField();
        search = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        table = new javax.swing.JTable();
        jButton9 = new javax.swing.JButton();
        txtbook = new javax.swing.JTextField();
        jButton10 = new javax.swing.JButton();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        txtnote = new javax.swing.JTextArea();
        butreset = new javax.swing.JButton();
        dateborrow = new com.toedter.calendar.JDateChooser();
        datedue = new com.toedter.calendar.JDateChooser();
        datereturn = new com.toedter.calendar.JDateChooser();
        butreset1 = new javax.swing.JButton();
        txtoverdue = new javax.swing.JTextField();
        butcalculate = new javax.swing.JButton();

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Check In / Out", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 24), new java.awt.Color(0, 0, 0))); // NOI18N

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(0, 0, 153));
        jLabel1.setText("ID");

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel2.setForeground(new java.awt.Color(0, 0, 153));
        jLabel2.setText("Book_ID");

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel3.setForeground(new java.awt.Color(0, 0, 153));
        jLabel3.setText("User_ID");

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel4.setForeground(new java.awt.Color(0, 0, 153));
        jLabel4.setText("Date Borrow");

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel5.setForeground(new java.awt.Color(0, 0, 153));
        jLabel5.setText("Date Return");

        jLabel6.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel6.setForeground(new java.awt.Color(0, 0, 153));
        jLabel6.setText("Overdue");

        jLabel7.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel7.setForeground(new java.awt.Color(0, 0, 153));
        jLabel7.setText("Fine");

        butInsert.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        butInsert.setForeground(new java.awt.Color(204, 0, 0));
        butInsert.setText("Insert");
        butInsert.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                butInsertActionPerformed(evt);
            }
        });

        butupdate.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        butupdate.setForeground(new java.awt.Color(204, 0, 0));
        butupdate.setText("Update");
        butupdate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                butupdateActionPerformed(evt);
            }
        });

        butdetele.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        butdetele.setForeground(new java.awt.Color(204, 0, 0));
        butdetele.setText("Delete");
        butdetele.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                butdeteleActionPerformed(evt);
            }
        });

        jLabel8.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel8.setForeground(new java.awt.Color(0, 0, 153));
        jLabel8.setText("Search by date borrow");

        search.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        search.setText("Find");
        search.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                searchActionPerformed(evt);
            }
        });

        table.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "ID", "Book_ID", "User_ID", "Date Borrow", "Date Due", "Date Return", "Overdue", "Fine", "Note"
            }
        ));
        table.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tableMouseClicked(evt);
            }
        });
        table.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tableKeyPressed(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                tableKeyReleased(evt);
            }
        });
        jScrollPane1.setViewportView(table);

        jButton9.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jButton9.setForeground(new java.awt.Color(204, 0, 0));
        jButton9.setText("Check");
        jButton9.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton9ActionPerformed(evt);
            }
        });

        txtbook.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtbookActionPerformed(evt);
            }
        });

        jButton10.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jButton10.setForeground(new java.awt.Color(204, 0, 0));
        jButton10.setText("Check");
        jButton10.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton10ActionPerformed(evt);
            }
        });

        jLabel9.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel9.setForeground(new java.awt.Color(0, 0, 153));
        jLabel9.setText("Date Due");

        jLabel10.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel10.setForeground(new java.awt.Color(0, 0, 153));
        jLabel10.setText("Note");

        txtnote.setColumns(20);
        txtnote.setRows(5);
        jScrollPane2.setViewportView(txtnote);

        butreset.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        butreset.setForeground(new java.awt.Color(204, 0, 0));
        butreset.setText("Reset");
        butreset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                butresetActionPerformed(evt);
            }
        });

        butreset1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        butreset1.setForeground(new java.awt.Color(204, 0, 0));
        butreset1.setText("Load");
        butreset1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                butreset1ActionPerformed(evt);
            }
        });

        butcalculate.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        butcalculate.setForeground(new java.awt.Color(204, 0, 0));
        butcalculate.setText("Calculate");
        butcalculate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                butcalculateActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(32, 32, 32)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4)
                            .addComponent(jLabel9))
                        .addGap(37, 37, 37)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(dateborrow, javax.swing.GroupLayout.PREFERRED_SIZE, 216, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                            .addComponent(txtuser, javax.swing.GroupLayout.DEFAULT_SIZE, 170, Short.MAX_VALUE)
                                            .addComponent(txtid)
                                            .addComponent(txtbook))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jButton10)
                                            .addComponent(jButton9))))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel5)
                                    .addComponent(jLabel10)
                                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                        .addComponent(jLabel7, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(jLabel6, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                                .addGap(37, 37, 37))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(datedue, javax.swing.GroupLayout.PREFERRED_SIZE, 216, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 240, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(txtoverdue, javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(txtfine, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 170, Short.MAX_VALUE))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(datereturn, javax.swing.GroupLayout.PREFERRED_SIZE, 213, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(34, 34, 34)
                                .addComponent(butcalculate)))
                        .addGap(48, 48, 48))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel8)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(aaa, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(search)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 146, Short.MAX_VALUE)
                        .addComponent(butreset1)
                        .addGap(18, 18, 18)
                        .addComponent(butreset)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(butInsert)
                        .addGap(18, 18, 18)
                        .addComponent(butupdate)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(butdetele)))
                .addContainerGap())
            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(txtid, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(jButton9)
                            .addComponent(txtbook, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtuser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3)
                            .addComponent(jButton10))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel4)
                            .addComponent(dateborrow, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel9)
                            .addComponent(datedue, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel5)
                                .addGap(18, 18, 18)
                                .addComponent(jLabel6)
                                .addGap(0, 0, Short.MAX_VALUE))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(butcalculate)
                                    .addComponent(datereturn, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 15, Short.MAX_VALUE)
                                .addComponent(txtoverdue, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(txtfine, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel7))))
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(11, 11, 11)
                                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(26, 26, 26)
                                .addComponent(jLabel10)))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 326, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(aaa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8)
                    .addComponent(search)
                    .addComponent(butInsert)
                    .addComponent(butupdate)
                    .addComponent(butdetele)
                    .addComponent(butreset)
                    .addComponent(butreset1))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 7, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void searchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_searchActionPerformed
        if (checkfind() == true) {
            search();
        }

    }//GEN-LAST:event_searchActionPerformed

    private void butInsertActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_butInsertActionPerformed
        if (input() == true) {
            JOptionPane.showMessageDialog(this, "Insert success");
            Insert(st);
        }
    }//GEN-LAST:event_butInsertActionPerformed

    private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton9ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jButton9ActionPerformed

    private void txtbookActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtbookActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtbookActionPerformed

    private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton10ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jButton10ActionPerformed

    private void tableKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tableKeyPressed
    }//GEN-LAST:event_tableKeyPressed

    private void tableKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tableKeyReleased
        int k1 = evt.getKeyCode();

        if (k1 == 38) {
            Click();

        } else if (k1 == 40) {
            Click();

        } else if (k1 == KeyEvent.VK_DELETE) {
            Delete();
        }
    }//GEN-LAST:event_tableKeyReleased
    int k = 0;
    private void tableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tableMouseClicked
        Click();
    }//GEN-LAST:event_tableMouseClicked

    private void butdeteleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_butdeteleActionPerformed
        Delete();
    }//GEN-LAST:event_butdeteleActionPerformed

    private void butresetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_butresetActionPerformed
        ClearFiled();

    }//GEN-LAST:event_butresetActionPerformed

    private void butupdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_butupdateActionPerformed

       
        if (update() == true) {
            JOptionPane.showMessageDialog(this, "Update success");
            update(st);
        }
        
    }//GEN-LAST:event_butupdateActionPerformed

    private void butreset1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_butreset1ActionPerformed
        loaddata();
    }//GEN-LAST:event_butreset1ActionPerformed

    private void butcalculateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_butcalculateActionPerformed
        int count = 0;
        int k = 0;
        if (k == 0) {
            Long start = StringToDate(DateToString(datedue.getDate()));
            Long end = StringToDate(DateToString(datereturn.getDate()));
            Long sum = ((end - start) / 86400000) / 365;

            while (start <= end) {
                Date start1 = new Date();
                start1.setTime(start);
                start += 86400000;
            }
            this.sum = sum - count;
           if ((sum - count) < 0) {
                JOptionPane.showMessageDialog(rootPane, "Please check the date !", "Please", JOptionPane.WARNING_MESSAGE);                
            } 
           else if ((sum - count) == 0) {
                JOptionPane.showMessageDialog(rootPane,"Number of days overdue :"+this.sum,"OverDue",JOptionPane.WARNING_MESSAGE);
            } else {
                 JOptionPane.showMessageDialog(rootPane,"Number of days overdue :"+this.sum,"OverDue",JOptionPane.WARNING_MESSAGE);
                this.t = this.sum * 5;
                 JOptionPane.showMessageDialog(rootPane,"Fines are :"+this.t+"Cent","Fine",JOptionPane.WARNING_MESSAGE);
            }
             
        }
    }//GEN-LAST:event_butcalculateActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(CheckInOut.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(CheckInOut.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(CheckInOut.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(CheckInOut.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new CheckInOut().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField aaa;
    private javax.swing.JButton butInsert;
    private javax.swing.JButton butcalculate;
    private javax.swing.JButton butdetele;
    private javax.swing.JButton butreset;
    private javax.swing.JButton butreset1;
    private javax.swing.JButton butupdate;
    private com.toedter.calendar.JDateChooser dateborrow;
    private com.toedter.calendar.JDateChooser datedue;
    private com.toedter.calendar.JDateChooser datereturn;
    private javax.swing.JButton jButton10;
    private javax.swing.JButton jButton9;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JButton search;
    private javax.swing.JTable table;
    private javax.swing.JTextField txtbook;
    private javax.swing.JTextField txtfine;
    private javax.swing.JTextField txtid;
    private javax.swing.JTextArea txtnote;
    private javax.swing.JTextField txtoverdue;
    private javax.swing.JTextField txtuser;
    // End of variables declaration//GEN-END:variables
}